package com.meilingcloud.biobank.controller;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.meilingcloud.biobank.domain.GeneralQuery;
import com.meilingcloud.biobank.domain.SampleBackup;
import com.meilingcloud.biobank.domain.dto.FuzzyQueryDto;
import com.meilingcloud.biobank.domain.dto.GeneralQueryDto;
import com.meilingcloud.biobank.domain.dto.GeneralQueryParamDto;
import com.meilingcloud.biobank.service.IGeneralQueryService;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.core.page.TableDataInfo;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.StringUtils;
import com.meilingcloud.common.utils.poi.ExcelUtil;
import com.meilingcloud.common.utils.sql.SqlUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/biobank/general")
public class GeneralQueryController extends BaseController {

    private final IGeneralQueryService generalQueryService;

    /**
     * 获取查询条件
     */
    @PreAuthorize("@ss.hasPermi('biobank:generalQuery:query')")
    @PostMapping("/getQueryParams")
    public AjaxResult getQueryParams(@RequestBody GeneralQueryParamDto dto) {
        return AjaxResult.success(generalQueryService.getQueryParams(dto));
    }

    /**
     * 搜索列表
     */
    @PreAuthorize("@ss.hasPermi('biobank:generalQuery:list')")
    @PostMapping("/queryList")
    public TableDataInfo queryList(@RequestBody GeneralQueryDto dto) {
        GeneralQuery generalQuery = generalQueryService.handleGeneralQuery(dto);
        if (ObjectUtil.isNotNull(dto.getPageNum()) && ObjectUtil.isNotNull(dto.getPageSize())) {
            String order;
            if (StringUtils.isEmpty(dto.getOrderByColumn())) {
                order = "";
            } else {
                order = StringUtils.toUnderScoreCase(dto.getOrderByColumn()) + " " + dto.getIsAsc();
            }
            String orderBy = SqlUtil.escapeOrderBySql(order);
            PageHelper.startPage(dto.getPageNum(), dto.getPageSize(), orderBy);
        }
        List<SampleBackup> sampleBackups = generalQueryService.queryList(generalQuery);
        return getDataTable(sampleBackups);
    }

    @PreAuthorize("@ss.hasPermi('biobank:generalQuery:export')")
    @Log(title = "综合查询 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody GeneralQueryDto dto) {
        GeneralQuery generalQuery = generalQueryService.handleGeneralQuery(dto);
        List<SampleBackup> list = generalQueryService.queryList(generalQuery);
        ExcelUtil<SampleBackup> util = new ExcelUtil<>(SampleBackup.class);
        return util.exportExcel(list, "样本复份数据");
    }

    /**
     * 模糊查询
     */
    @PreAuthorize("@ss.hasPermi('biobank:generalQuery:list')")
    @GetMapping("/fuzzyQuery")
    public TableDataInfo fuzzyQuery(FuzzyQueryDto dto) {
        startPage();
        return getDataTable(generalQueryService.fuzzyQueryList(dto.getKeyword()));
    }

}
